#region "Copyright (C) Lenny Granovsky. 2005-2013"
//This program is free software distribute under the terms of the GNU General Public License as published by
//the Free Software Foundation. Please read AssemblyInfo.cs file for more information.
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Web.Services.Protocols;
using System.Web;
using System.Web.Caching;

namespace Web.Enhancements.Rest.InfoCaching
{
    [Serializable()]
    internal class MethodInfoCache : Dictionary<string, MethodInfoCacheItem>
    {
        public const string MemorySlotName = "WebRestMethodInfoCache";
        private const string LastCachedDateTimeHolder = "LastCachedDateTimeHolder";

        public static DateTime LastCachedDateTime
        {
            get
            {
                if (HttpContext.Current != null)
                {
                    if (HttpContext.Current.Application[LastCachedDateTimeHolder] != null)
                        return (DateTime)HttpContext.Current.Application[LastCachedDateTimeHolder];
                }
                return DateTime.MinValue;
            }
        }

        public static MethodInfoCache Current
        {
            get
            {
                object tmp = null;
                if (WebRestSettings.Current.InfoCacheLocation == ClassMethodInfoCacheLocation.ApplicationCache)
                {
                    tmp = HttpRuntime.Cache.Get(MemorySlotName);
                    if (tmp == null)
                    {
                        tmp = new MethodInfoCache();
                        HttpRuntime.Cache.Insert(MemorySlotName, tmp, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
                        HttpContext.Current.Application[LastCachedDateTimeHolder] = DateTime.Now;
                    }
                }
                else
                {
                    tmp = HttpContext.Current.Application[MemorySlotName];
                    if (tmp == null)
                    {
                        tmp = new MethodInfoCache();
                        HttpContext.Current.Application[MemorySlotName] = tmp;
                    }
               }
                return (MethodInfoCache)tmp;
            }
            set
            {
                if (WebRestSettings.Current.InfoCacheLocation == ClassMethodInfoCacheLocation.ApplicationCache)
                    HttpRuntime.Cache.Insert(MemorySlotName, value, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
                else
                    HttpContext.Current.Application[MemorySlotName] = value;
                //set datetime
                HttpContext.Current.Application[LastCachedDateTimeHolder] = DateTime.Now;
            }
        }

        public MethodInfoCache():base()
        {
        }

        public static string CreateKey(string className, string methodName)
        {
            return string.Format("{0}_{1}", className.ToLower(), methodName.ToLower());
        }
    }
}
